
var default_charset = "utf-8";

/* ---------------------------------------------------------------------------

Sugar Folder Class

--------------------------------------------------------------------------- */

var SugarFolder = function () {

  this.fso = new ActiveXObject("Scripting.FileSystemObject");
  this.folder = this.fso.getFolder(".");
  this.files = [];
  this.getFiles();

}

SugarFolder.prototype.getFiles = function ( filename ) {

  var i = new Enumerator( this.folder.files );

  for ( ; !i.atEnd(); i.moveNext() ) {

    this.files[ this.files.length ] = new SugarFile( i.item().path, i.item().name )

  }

}

SugarFolder.prototype.getFileByFilename = function ( filename ) {

  for ( var i in this.files ) {

    if( this.files[i].filename == filename ) {

      return this.files[i];

    }

  }

}

SugarFolder.prototype.create = function ( filename, str, charset ) {

  var filepath = this.folder.path + "\\" + filename;
  this.fso.CreateTextFile(filepath);
  this.getFiles();

  if ( str ) {

    var file = this.getFileByFilename( filename );
    file.setCharset( charset );
    file.innerStr( str );

  }

}

/* ---------------------------------------------------------------------------

Sugar File Class

--------------------------------------------------------------------------- */

var SugarFile = function ( path, filename ) {
  
  this.path = path;
  this.filename = filename;
  this.charset = "";
  this.str;

}

SugarFile.prototype.setCharset = function ( charset ) {

  this.charset = charset;

}

SugarFile.prototype.innerStr = function ( str ) {

  if ( !str && this.str ) {

    return this.str;

  }

  var handler;

  handler = new ActiveXObject('ADODB.Stream');
  handler.type = 2;
  if ( this.charset ) {
    handler.charset = this.charset;
  }
  else {
    handler.charset = default_charset;
  }

  handler.open();

  if ( str ) {

    handler.writeText( str );
    handler.saveToFile( this.path, 2 );
    handler.close();

    this.str = str;

  }
  else {

    handler.loadFromFile( this.path );
    this.str = handler.readText( -1 );
    handler.close();

    return this.str;

  }

}

SugarFile.prototype.csv = function () {

  var connect = new ActiveXObject("ADODB.Connection");

  connect.Provider = "Microsoft.Jet.OLEDB.4.0";
  connect.Properties("Data Source") = ".";
  connect.Properties("Extended Properties") = "TEXT;HDR=No;FMT=Delimited";
  connect.Open();
      
  var cursor = connect.Execute("SELECT * FROM " + this.filename);
  var recs = [];
  var recs_count = 0;

  for ( ; !cursor.EOF; cursor.MoveNext() ) {

    recs[ recs_count ] = [];

    for (var i = 0; i < cursor.fields.Count; i++) {
        
      recs[ recs_count ][i] = cursor.fields.Item(i).Value;

    }

    recs_count++;

  }

  connect.Close();

  return recs;

}

/* ---------------------------------------------------------------------------

Create Folder

--------------------------------------------------------------------------- */

var folder = new SugarFolder();
